package ext.acae2.statements;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import wt.fc.QueryResult;
import wt.log4j.LogR;
import wt.part.Quantity;
import wt.part.WTPart;
import wt.part.WTPartConfigSpec;
import wt.part.WTPartHelper;
import wt.part.WTPartStandardConfigSpec;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTRuntimeException;
import wt.vc.views.ViewHelper;
import ext.acae2.statements.bean.StructuralDetailBean;
import ext.acae2.util.IBAUtil;

public class StructuralDetail {

	private static final Logger LOGGER = LogR.getLogger(StructuralDetail.class.getName());
	
	public static void download(WTPart part, OutputStream output) 
            throws WTRuntimeException, WTException, IOException {
        LOGGER.debug("> Start download");
        
        String wthome = (String) WTProperties.getLocalProperties().get("wt.home");
        
        File templateFile = new File(wthome + File.separator 
                + "codebase" + File.separator + "ext" + File.separator 
                + "acae2" + File.separator + "conf" + File.separator
                + "StructuralDetail.xls");
        
        int seq = 0;
        int quantity = 1;
        
        if((part.getNumber().length() == 13 && part.getNumber().substring(10,11).equals("M")) || (part.getNumber().length() == 13 && part.getNumber().substring(10,11).equals("G"))){
        	List<StructuralDetailBean> structuralDetailList = new ArrayList<StructuralDetailBean>();
            creatStructuralDetailBean(part, structuralDetailList, seq, quantity);
            
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(templateFile));
            HSSFSheet sheet = workbook.getSheetAt(0);
            
            int rowNum = 2;
            write(structuralDetailList, rowNum, sheet);
            
            workbook.write(output);
            output.flush();
            output.close();
        }
        
        LOGGER.debug("> End download");
    }
	
	private static void creatStructuralDetailBean(WTPart part, List<StructuralDetailBean> structuralDetailList, int seq, int quant) throws RemoteException, WTException{
		
		WTPartStandardConfigSpec standardConfigSpec = WTPartStandardConfigSpec
				.newWTPartStandardConfigSpec(
						ViewHelper.service.getView("Design"), null);
		WTPartConfigSpec configSpec = WTPartConfigSpec
				.newWTPartConfigSpec(standardConfigSpec);
		
			StructuralDetailBean structuralDetailBean = new StructuralDetailBean();
			structuralDetailBean.setSeq(seq+"");
			structuralDetailBean.setNumber(part.getNumber());
			structuralDetailBean.setName(part.getName());
			structuralDetailBean.setVersion(part.getVersionIdentifier().getValue());
			structuralDetailBean.setState(part.getLifeCycleState().getDisplay());
			structuralDetailBean.setQuantity(quant+"");
			IBAUtil iba = new IBAUtil(part);
			structuralDetailBean.setIba(iba);
			structuralDetailList.add(structuralDetailBean);
			seq++;
			
			QueryResult childPartsQs = WTPartHelper.service.getUsesWTParts(part, configSpec);
			while (childPartsQs.hasMoreElements()) {
				
				Object obj[] = null;
				obj = (Object[]) childPartsQs.nextElement();
				if (obj[1] instanceof WTPart) {
					WTPart childPart = (WTPart) obj[1];
							
					WTPartUsageLink alink = (WTPartUsageLink) obj[0];
					Quantity quantity = alink.getQuantity();
							
					double singleQuantity = quantity.getAmount();
					String singleStr = String.valueOf(singleQuantity).substring(0,String.valueOf(singleQuantity).lastIndexOf(".")).toString();
					
					creatStructuralDetailBean(childPart, structuralDetailList, seq, Integer.valueOf(singleStr));
				}
			}
	}
	
	private static void write(List<StructuralDetailBean> structuralDetailList, int rowNum, HSSFSheet sheet){
		for (StructuralDetailBean structuralDetail : structuralDetailList) {
			HSSFRow row = sheet.createRow(rowNum);
            
			int i = 0;
            row.createCell(i).setCellValue(structuralDetail.getSeq());
            i++;
            row.createCell(i).setCellValue(structuralDetail.getNumber());
            i++;
            row.createCell(i).setCellValue(structuralDetail.getName());
            i++;
            row.createCell(i).setCellValue(structuralDetail.getVersion());
            i++;
            row.createCell(i).setCellValue(structuralDetail.getState());
            i++;
            row.createCell(i).setCellValue(structuralDetail.getQuantity());
            i++;
            
            // 软属性
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("drawingNo")); // 图号
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("secretLevel")); // 密级
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("securityDeadline")); // 保密期限
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("copyRight")); // 设计单位
            i++;
            if(structuralDetail.getIba().getIBAValue("isMaturePart") != null){
            	if(structuralDetail.getIba().getIBAValue("isMaturePart").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是阶段设计
                }else if(structuralDetail.getIba().getIBAValue("isMaturePart").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否阶段设计
                }
            }
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("characterType")); // 特性分类
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("mass")); // 质量
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("unit")); // 质量单位
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("material")); // 材料牌号
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("materialStandard")); // 材料技术标准
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("blankSpec")); // 材料规格
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("blankState")); // 材料状态
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("blankType")); // 毛坯类型
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("blankStandard")); // 毛坯标准
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("materialType")); // 原材料类型
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("heatTreatment")); // 热处理
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("surfaceTreatment")); // 表面处理
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("losslessDetectMethod")); // 无损检测方法及标准
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("specialProcess")); // 特种工艺
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("deliveryRequest")); // 验收交付要求
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("model")); // 型号
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("stageID")); // 阶段标识
            i++;
            row.createCell(i).setCellValue(structuralDetail.getIba().getIBAValue("batch")); // 批次
            i++;
            if(structuralDetail.getIba().getIBAValue("allowExpand") != null){
            	if(structuralDetail.getIba().getIBAValue("allowExpand").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是结构可拆
                }else if(structuralDetail.getIba().getIBAValue("allowExpand").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否结构可拆
                }
            }
            i++;
            if(structuralDetail.getIba().getIBAValue("isStandard") != null){
            	if(structuralDetail.getIba().getIBAValue("isStandard").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是标准件
                }else if(structuralDetail.getIba().getIBAValue("isStandard").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否标准件
                }
            }
            i++;
            if(structuralDetail.getIba().getIBAValue("isSelfDesign") != null){
            	if(structuralDetail.getIba().getIBAValue("isSelfDesign").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是自设件
                }else if(structuralDetail.getIba().getIBAValue("isSelfDesign").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否自设件
                }
            }
            i++;
            if(structuralDetail.getIba().getIBAValue("isUniversal") != null){
            	if(structuralDetail.getIba().getIBAValue("isUniversal").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是通用件
                }else if(structuralDetail.getIba().getIBAValue("isUniversal").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否通用件
                }
            }
            i++;
            if(structuralDetail.getIba().getIBAValue("isPurchase") != null){
            	if(structuralDetail.getIba().getIBAValue("isPurchase").equals("真")){
                	row.createCell(i).setCellValue("Y"); // 是成附件
                }else if(structuralDetail.getIba().getIBAValue("isPurchase").equals("假")){
                	row.createCell(i).setCellValue("N"); // 否成附件
                }
            }
            i++;
            
            rowNum++;
		}
	}
	
}
